﻿using CallCenter.Data.Base;
using CallCenter.Data.Models;
using CallCenter.Data.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace CallCenter.Data.BizUtilities
{
   public class DurationUnitUtility:BaseUtility
    {
        public DurationUnitUtility(DataManager manager)
            : base(manager)
        { }


        public List<DurationUnitViewModel> GetAll() //Gets all the data form the contacts table
        {
            string sql = "Select * from ccDurationUnits";

            return this.Factory.ExecuteObjects<DurationUnitViewModel>(sql);
        }


    



        public ReturnMessage<DurationUnitViewModel> InsertToDurationd(DurationUnitViewModel model)
        {
            var returnMessage = new ReturnMessage<DurationUnitViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Insert(model.durationUnitsCode,model.durationUnitsName,model.description);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public ReturnMessage<DurationUnitViewModel> UpdateToDuration(DurationUnitViewModel model)
        {
            var returnMessage = new ReturnMessage<DurationUnitViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Update(model.id,model.durationUnitsCode,model.durationUnitsName,model.description);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public int Insert(string durationUnitsCode, string durationUnitsName, string description) //insert data to a contact tab
        {
            int nRtn = -1;
            string sql = "INSERT INTO ccDurationUnits (durationUnitsCode, durationUnitsName, description) Values(@durationUnitsCode, @durationUnitsName, @description) SELECT @pResult =@@IDENTITY";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@durationUnitsCode", SqlDbType.NVarChar, 10, durationUnitsCode);
                this.Factory.AddParameter(cmd, "@durationUnitsName", SqlDbType.NVarChar, 100, durationUnitsName);
                this.Factory.AddParameter(cmd, "@description", SqlDbType.NVarChar, 100, description);


                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }
        public int Update(
           int id,
           string durationUnitsCode,
           string durationUnitsName,
           string description) //insert data to a contact table
        {
            int nRtn = -1;
            string sql = "UPDATE ccDurationUnits " +
                " SET durationUnitsCode=@durationUnitsCode, durationUnitsName=@durationUnitsName, description=@description WHERE id=@id";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@durationUnitsCode", SqlDbType.NVarChar, 10, durationUnitsCode);
                this.Factory.AddParameter(cmd, "@durationUnitsName", SqlDbType.NVarChar, 100, durationUnitsName);
                this.Factory.AddParameter(cmd, "@description", SqlDbType.NVarChar, 100, description);
                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

            }
            nRtn = id;
            return nRtn; //retruning the data stored in the table
        }


        public void Delete(int nUnt) //delete the row of data using the id passed to the function
        {
            string sql = "DELETE FROM ccDurationUnits " +
                  "WHERE ID = @ID ";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, nUnt);
                this.Factory.ExecuteNonQuery(cmd);
            }

        }

        public void DeleteAll() //delete all the rows in the data.
        {
            string sql = "DELETE FROM ccDurationUnits ";
            this.Factory.ExecuteTable(sql);

        }




    }
}
